package com.heima.media.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.media.dto.ChannelDto;
import com.heima.media.entity.WmChannel;
import com.heima.media.mapper.WmChannelMapper;
import com.heima.media.service.IWmChannelService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

@Service
public class IWmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements IWmChannelService {

    /**
     * 根据id删除频道
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteChannel(Integer id) {
        this.removeById(id);
        return ResponseResult.okResult();
    }

    /**
     * 修改频道
     *
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult updateChannel(WmChannel wmChannel) {
        //判断参数是否为空
        if (wmChannel == null || StringUtils.isEmpty(wmChannel.getName())) {
            //为空提示参数为无效
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断是否存在
        //两种可能,id和频道名称相同可以修改
        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();
         queryWrapper.eq(WmChannel::getName, wmChannel.getName());
         //得到数据库对象
        WmChannel channel = this.getOne(queryWrapper);
        //id不同,频道相同不能修改
        //判断,如果不为空,且id不同则不能修改
        if(channel != null && !channel.getId().equals(wmChannel.getId())){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        this.updateById(wmChannel);
        return ResponseResult.okResult();
    }

    /**
     * 添加频道
     *
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult saveChannel(WmChannel wmChannel) {
        //判断参数是否为空
        if (wmChannel == null || StringUtils.isEmpty(wmChannel.getName())) {
            //为空提示参数为无效
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断是否存在
        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmChannel::getName,wmChannel.getName());
        int count = this.count(queryWrapper);
        if(count>0){
            //存在则返回提示信息
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //不存在则新增
        wmChannel.setCreatedTime(new Date());
        this.save(wmChannel);
        //返回成功信息
        return ResponseResult.okResult();
    }

    /**
     * 分页查询频道列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult listByPage(ChannelDto dto) {

        //构建查询条件
        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();
        //判断是否为空,为空查询所有,不为空则设置查询名字
        if (!StringUtils.isEmpty(queryWrapper)) {
            queryWrapper.like(WmChannel::getName, dto.getName());
        }
        //构建分页条件
        IPage<WmChannel> channelPage = new Page<>(dto.getPage(), dto.getSize());
        IPage<WmChannel> page = this.page(channelPage, queryWrapper);
        //返回响应结果
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), page.getTotal(), page.getRecords());
        return result;

    }
}
